package com.barunsw.bwf.dhtmlx_sample;

import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
@RequestMapping("/dhtmlx")
public class DhtmlxSampleController {
	private static final Logger LOGGER = LoggerFactory.getLogger(DhtmlxSampleController.class);
	
	@Autowired
	DhtmlxSampleService dhtmlxSampleService;
	
	@RequestMapping(value="", method=RequestMethod.GET)
	public @ResponseBody List<ContactVo> getContactList() throws Exception {
		LOGGER.debug("getContactList");
		List<ContactVo> contactList = dhtmlxSampleService.selectList();
		return contactList;
	}
	
	@RequestMapping(value="", method=RequestMethod.POST)
	public @ResponseBody ResultVo insertContact(@RequestBody ContactVo contactVo) throws Exception {
		LOGGER.debug(String.format("fname:%s, lname:%s, email:%s", 
				contactVo.getFname(), contactVo.getLname(), contactVo.getEmail()));
		
		ResultVo resultVo = new ResultVo(Result.OK, "");
		
		int result = dhtmlxSampleService.insert(contactVo);
		if ( result <= 0 ) {
			resultVo.setResult(Result.NOK);
			resultVo.setReason("Contact 정보를 추가하지 못했습니다.");
		}
		
		return resultVo;
	}
}
